package com.example.ywj.toutiao.utils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.ywj.toutiao.R;

import java.util.List;

public class PicAdapter extends BaseAdapter {
    public List<String> list;
    public PicAdapter(List<String> list){
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.pic_item, null);
            viewHolder = new ViewHolder();
            viewHolder.iv = convertView.findViewById(R.id.pic_item);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
        String url = Constant.BASEURL+"public/images/"+list.get(position)+".jpg";
        Glide.with(parent.getContext()).load(url).apply(requestOptions).into(viewHolder.iv);
        return convertView;
    }
    class ViewHolder{
        ImageView iv;
    }
}
